package com.longge;

import java.util.PriorityQueue;
import java.util.Queue;

class KthLargest {

    private final Queue<Integer> pq = new PriorityQueue<>();

    private final int k;

    public KthLargest(int k, int[] nums) {
        for (int num : nums) {
            pq.offer(num);
        }
        this.k = k;
    }

    public int add(int val) {
        pq.offer(val);
        while (pq.size() > k) {
          pq.poll();
        }
        return pq.peek();
    }

    public static void main(String[] args) {
        int add = new KthLargest(3, new int[]{4, 5, 8, 2}).add(3);
        System.out.println(add);
    }


}